<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\mall\controller;

use app\mall\base\MallFrontBase;

use app\mall\model\MallGoods;

class Goods extends MallFrontBase{
	/**
     * 产品信息页
     * @access public
     * @return \think\Response
     */
    public function info($id = 0, MallGoods $mall_goods){
        // 判断参数是否合法
        $id = (int)$id;
        if(!$id){
            abort(404, '缺少参数：id');
        }
        // 读取商品信息
        $goods = $mall_goods->getFrontGoodsInfo($id);
        if(!$goods){
            abort(404, '产品不存在或产品未上架');
        }
        // 获取产品的规格分类
        $goods_spec_cat = [];
        if(is_array($goods['goods_data']['goods_spec']) && count($goods['goods_data']['goods_spec'])){
            $goods_spec_cat = cache('mall_goods_spec_cat_'.key($goods['goods_data']['goods_spec']));
        }
        // 过滤未使用的规格分组与规格值
        if($goods_spec_cat){
            foreach ($goods_spec_cat['groups'] as $group_k => $group_v) {
                foreach ($group_v['items'] as $item_k => $item_v) {
                    if(!isset($goods['goods_data']['goods_spec'][$goods_spec_cat['id']][$item_v['id']])){
                        unset($goods_spec_cat['groups'][$group_k]['items'][$item_k]);
                    }
                }
                if(count($goods_spec_cat['groups'][$group_k]['items'])<1){
                    unset($goods_spec_cat['groups'][$group_k]);
                }
            }
        }
        // 模板变量赋值
        $this->assign($goods);
    	$this->assign([
            'seo_title'         => '首页',
            'seo_keywords'      => '关键词',
            'seo_description'   => '描述',
            'goods_spec_groups'  => isset($goods_spec_cat['groups']) ? $goods_spec_cat['groups'] : []
        ]);

        return $this->getTemplate();
    }
}